﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;
using Telerik.Web.UI;
using System.Net.Mail;

namespace HCFramework.AdminTool.ConfigManage
{
    public partial class UsrConfig : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void RadGridMain_PreRender(object sender, EventArgs e)
        {
           HCDataContext dc = new HCDataContext();
            RadGridMain.DataSource = dc.Configs.Where(ik=>ik.Name.Contains(txtFilterByName.Text));
            RadGridMain.DataBind();
        }

        protected void RadGridMain_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Footer)
            {
                HCDataContext dc = new HCDataContext();
                List<string> listNameInDatabase = dc.Configs.Select(ik => ik.Name).ToList();
                List<string> listNameInEnum = System.Enum.GetNames(typeof(E.Config)).ToList();
                DropDownList cboName = (DropDownList) e.Item.FindControl("cboName");
                cboName.DataSource = listNameInEnum.Except(listNameInDatabase);
                cboName.DataBind();
            }
        }

        protected void RadGridMain_ItemCommand(object source, GridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Them":
                    Config config = new Config();
                    config.Name = (e.Item.FindControl("cboName") as DropDownList).SelectedValue;
                    if (String.IsNullOrEmpty(config.Name)) return;
                    config.Value = (e.Item.FindControl("txtValue") as TextBox).Text;
                    C.ConfigController.Insert(config);
                    break;
                case "Sua":
                    HCDataContext dc = new HCDataContext();
                    Config config1 = C.ConfigController.GetByID(int.Parse(e.CommandArgument.ToString()), dc);
                    config1.Value = (e.Item.FindControl("txtValue") as TextBox).Text;
                    C.ConfigController.Update(config1, dc);
                    break;
                case "Xoa":
                    C.ConfigController.Delete(int.Parse(e.CommandArgument.ToString()));
                    break;
            }
        }
    }
}